<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{number}}.{{title}} - OnlineJudge</title>
    
    <!-- 引入ACE插件 -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ace.js" type="text/javascript" charset="utf-8"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ext-language_tools.js" type="text/javascript" charset="utf-8"></script>
    <!-- 引入jquery CDN -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    
    <!-- 引入字体图标 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">

    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        html,
        body {
            width: 100%;
            height: 100%;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f5f9f8;
        }

        .container {
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        .navbar {
            width: 100%;
            height: 60px;
            background: linear-gradient(135deg, #1a936f 0%, #0d7a5a 100%);
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            display: flex;
            align-items: center;
            padding: 0 20px;
            position: fixed;
            top: 0;
            z-index: 1000;
        }

        .navbar a {
            display: inline-block;
            padding: 0 20px;
            color: white;
            font-size: 16px;
            line-height: 60px;
            text-decoration: none;
            transition: all 0.3s ease;
            font-weight: 500;
        }

        .navbar a:hover {
            background-color: rgba(255, 255, 255, 0.15);
            transform: translateY(-2px);
        }

        .navbar .login {
            margin-left: auto;
            background-color: rgba(255, 255, 255, 0.2);
            border-radius: 4px;
            height: 36px;
            line-height: 36px;
            margin-top: 12px;
        }

        .navbar .login:hover {
            background-color: rgba(255, 255, 255, 0.3);
        }

        .main-content {
            margin-top: 60px;
            flex: 1;
            display: flex;
            flex-direction: column;
        }

        .part1 {
            display: flex;
            flex: 1;
            min-height: 500px;
            margin: 20px;
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
        }

        .left_desc {
            flex: 1;
            background-color: white;
            padding: 20px;
            overflow-y: auto;
            border-right: 1px solid #e0e7e5;
        }

        .question-header {
            padding-bottom: 15px;
            border-bottom: 2px solid #1a936f;
            margin-bottom: 20px;
        }

        .question-header h3 {
            color: #1a936f;
            font-size: 24px;
            font-weight: 600;
            margin-bottom: 5px;
        }

        .difficulty {
            display: inline-block;
            padding: 3px 10px;
            border-radius: 15px;
            font-size: 14px;
            font-weight: 500;
            margin-top: 5px;
        }

        .difficulty-easy {
            background-color: #e8f6f2;
            color: #2ecc71;
        }

        .difficulty-medium {
            background-color: #fef9e7;
            color: #f39c12;
        }

        .difficulty-hard {
            background-color: #fdedec;
            color: #e74c3c;
        }

        .question-desc {
            line-height: 1.6;
            color: #2c3e50;
        }

        .question-desc pre {
            background-color: #f8f9fa;
            padding: 15px;
            border-radius: 5px;
            overflow-x: auto;
            margin: 15px 0;
            border: 1px solid #e9ecef;
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
        }

        .right_code {
            flex: 1;
            display: flex;
            flex-direction: column;
            background-color: #2d2d2d;
            border-radius: 0 10px 10px 0;
            overflow: hidden;
        }

        .code-header {
            background-color: #1a936f;
            color: white;
            padding: 10px 15px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .code-header h4 {
            font-weight: 500;
            font-size: 16px;
        }

        .theme-selector {
            background: rgba(255, 255, 255, 0.2);
            border: none;
            color: white;
            padding: 5px 10px;
            border-radius: 4px;
            cursor: pointer;
        }

        #code {
            flex: 1;
            min-height: 400px;
        }

        .part2 {
            margin: 0 20px 20px;
            display: flex;
            flex-direction: column;
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
            overflow: hidden;
        }

        .result-header {
            background-color: #1a936f;
            color: white;
            padding: 12px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .result-header h3 {
            font-weight: 500;
            font-size: 18px;
        }

        .btn-submit {
            background: linear-gradient(135deg, #1a936f 0%, #0d7a5a 100%);
            color: white;
            border: none;
            padding: 10px 25px;
            border-radius: 5px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .btn-submit:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(26, 147, 111, 0.3);
        }

        .btn-submit:active {
            transform: translateY(0);
        }

        .result-content {
            padding: 20px;
            min-height: 150px;
            max-height: 300px;
            overflow-y: auto;
        }

        .result-default {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100%;
            color: #7f8c8d;
        }

        .result-default i {
            font-size: 40px;
            margin-bottom: 10px;
            color: #bdc3c7;
        }

        .result-item {
            margin-bottom: 15px;
            padding-bottom: 15px;
            border-bottom: 1px solid #ecf0f1;
        }

        .result-item:last-child {
            margin-bottom: 0;
            border-bottom: none;
        }

        .result-status {
            font-weight: 600;
            margin-bottom: 5px;
        }

        .status-success {
            color: #2ecc71;
        }

        .status-error {
            color: #e74c3c;
        }

        .status-warning {
            color: #f39c12;
        }

        .result-output {
            background-color: #f8f9fa;
            padding: 10px;
            border-radius: 5px;
            margin-top: 8px;
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            font-size: 14px;
            white-space: pre-wrap;
            overflow-x: auto;
        }

        .footer {
            width: 100%;
            height: 60px;
            text-align: center;
            line-height: 60px;
            color: #7f8c8d;
            background-color: #f1f5f4;
            border-top: 1px solid #e0e7e5;
            font-size: 14px;
        }

        /* 响应式设计 */
        @media (max-width: 1024px) {
            .part1 {
                flex-direction: column;
                margin: 10px;
            }
            
            .left_desc {
                max-height: 40vh;
                border-right: none;
                border-bottom: 1px solid #e0e7e5;
            }
            
            .right_code {
                border-radius: 0 0 10px 10px;
            }
        }

        @media (max-width: 768px) {
            .navbar a {
                padding: 0 12px;
                font-size: 14px;
            }
            
            .question-header h3 {
                font-size: 20px;
            }
            
            .part2 {
                margin: 0 10px 10px;
            }
            
            .result-header {
                flex-direction: column;
                gap: 10px;
                align-items: flex-start;
            }
            
            .btn-submit {
                align-self: flex-end;
            }
        }
    </style>
</head>

<body>
    <div class="container">
        <!-- 导航栏 -->
        <div class="navbar">
            <a href="/">首页</a>
            <a href="/all_questions">题库</a>
            <a class="login" href="#"><i class="fas fa-sign-in-alt"></i> 登录</a>
        </div>
        
        <div class="main-content">
            <!-- 左右呈现，题目描述和预设代码 -->
            <div class="part1">
                <div class="left_desc">
                    <div class="question-header">
                        <h3><span id="number">{{number}}</span>. {{title}}</h3>
                        <div class="difficulty difficulty-{{difficulty_class}}">{{star}}</div>
                    </div>
                    <div class="question-desc">
                        <pre>{{desc}}</pre>
                    </div>
                </div>
                <div class="right_code">
                    <div class="code-header">
                        <h4><i class="fas fa-code"></i> 代码编辑器</h4>
                        <select class="theme-selector" id="themeSelector">
                            <option value="monokai">C++</option>
                            <option value="github">GitHub</option>
                            <option value="tomorrow">Tomorrow</option>
                            <option value="solarized_light">Solarized Light</option>
                        </select>
                    </div>
                    <div id="code" class="ace_editor">{{header}}</div>
                </div>
            </div>
            
            <!-- 提交并且得到结果，并显示 -->
            <div class="part2">
                <div class="result-header">
                    <h3><i class="fas fa-clipboard-check"></i> 运行结果</h3>
                    <button class="btn-submit" onclick="submit()">
                        <i class="fas fa-paper-plane"></i> 提交代码
                    </button>
                </div>
                <div class="result-content">
                    <div class="result-default" id="defaultResult">
                        <i class="fas fa-code"></i>
                        <p>点击"提交代码"按钮查看运行结果</p>
                    </div>
                    <div class="result-details" id="resultDetails" style="display: none;"></div>
                </div>
            </div>
        </div>
        
        <div class="footer">
            <p>© OnlineJudge 在线评测系统 | 保留所有权利</p>
        </div>
    </div>

    <script>
        // 初始化ACE编辑器
        var editor = ace.edit("code");
        editor.setTheme("ace/theme/monokai");
        editor.session.setMode("ace/mode/c_cpp");
        editor.setFontSize(14);
        editor.getSession().setTabSize(4);
        editor.setReadOnly(false);
        
        // 启用提示菜单
        ace.require("ace/ext/language_tools");
        editor.setOptions({
            enableBasicAutocompletion: true,
            enableSnippets: true,
            enableLiveAutocompletion: true
        });
        
        // 主题切换功能
        document.getElementById('themeSelector').addEventListener('change', function() {
            editor.setTheme('ace/theme/' + this.value);
        });

        function submit(){
            // 隐藏默认结果提示
            document.getElementById('defaultResult').style.display = 'none';
            document.getElementById('resultDetails').style.display = 'block';
            
            // 1. 收集当前页面的有关数据, 1. 题号 2.代码
            var code = editor.getSession().getValue();
            var number = document.getElementById('number').textContent;
            var judge_url = "/judge/" + number;
            
            // 显示加载状态
            var resultDetails = document.getElementById('resultDetails');
            resultDetails.innerHTML = '<div class="result-item"><div class="result-status status-warning"><i class="fas fa-spinner fa-spin"></i> 代码运行中，请稍候...</div></div>';
            
            // 2. 构建json，并通过ajax向后台发起基于http的json请求
            $.ajax({
                method: 'POST',
                url: judge_url,
                dataType: 'json',
                contentType: 'application/json;charset=utf-8',
                data: JSON.stringify({
                    'code': code,
                    'input': ''
                }),
                success: function(data){
                    show_result(data);
                },
                error: function(xhr, status, error) {
                    resultDetails.innerHTML = '<div class="result-item"><div class="result-status status-error"><i class="fas fa-exclamation-circle"></i> 请求失败: ' + error + '</div></div>';
                }
            });
        }
        
        function show_result(data) {
            var resultDetails = document.getElementById('resultDetails');
            var html = '';
            
            // 首先拿到结果的状态码和原因结果
            var _status = data.status;
            var _reason = data.reason;
            
            // 根据状态码设置状态样式
            var statusClass = _status === 0 ? 'status-success' : 'status-error';
            var statusIcon = _status === 0 ? 'fa-check-circle' : 'fa-exclamation-circle';
            var statusText = _status === 0 ? '运行成功' : '运行错误';
            
            html += '<div class="result-item">';
            html += '<div class="result-status ' + statusClass + '"><i class="fas ' + statusIcon + '"></i> ' + statusText + '</div>';
            html += '<div>' + _reason + '</div>';
            
            if(_status === 0) {
                // 请求是成功的，编译运行过程没出问题
                var _stdout = data.stdout || '无输出';
                var _stderr = data.stderr || '';
                
                html += '<div class="result-output"><strong>标准输出:</strong><br>' + _stdout + '</div>';
                
                if(_stderr) {
                    html += '<div class="result-output"><strong>标准错误:</strong><br>' + _stderr + '</div>';
                }
            } else {
                // 编译运行出错
                html += '<div class="result-output"><strong>错误信息:</strong><br>' + (_reason || '未知错误') + '</div>';
            }
            
            html += '</div>';
            resultDetails.innerHTML = html;
            
            // 滚动到结果区域
            resultDetails.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
        }
    </script>
</body>

</html>